﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class ItNController : Controller
    {
        //
        // GET: /ItN/

        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSCapTinHoc
                            // where dt.IDCanBo == id
                            select dt).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,

                                 customer.Ten
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {
            List<CapTinHoc> DSDT = CapTinHocModels.GetDSCapTinHoc();
            ViewBag.DSCapTinHoc = DSDT;
            return View();
        }
        public ActionResult Add()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Add(FormCollection fm)
        {
            string ten = fm["Ten"];
            CapTinHoc obj = new CapTinHoc();
            //gan vao bang
            obj.Ten = ten;
            CapTinHocModels.Add(obj);

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {

            CapTinHocModels.Delete(id);
            return RedirectToAction("Index");
        }


        public ActionResult Update(int id)
        {

            CapTinHoc list = CapTinHocModels.GetChiTietDSCapTinHoc(id);
            ViewBag.DSCapTinHoc = list;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection fm, int id)
        {

            string ten = fm["Ten"];
            CapTinHoc list = CapTinHocModels.GetChiTietDSCapTinHoc(id);
            list.Ten = ten;
            CapTinHocModels.Edit(list);
            return RedirectToAction("Index");
        }
        public int GetConfirmDelete(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from cb in context.DSCanBo
                        where cb.TrinhDoTinHoc == id
                        select cb;
            return query.ToList().Count;
        }
    }
}
